package com.peng.sell.service.impl;

import com.alibaba.fastjson.JSON;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.BestPayService;
import com.lly835.bestpay.service.impl.WxPayServiceImpl;
import com.lly835.bestpay.utils.JsonUtil;
import com.peng.sell.dto.OrderDTO;
import com.peng.sell.enums.ResponseCodeEnum;
import com.peng.sell.exception.SellException;
import com.peng.sell.service.OrderService;
import com.peng.sell.service.PayService;
import com.peng.sell.utils.BigDecimalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    BestPayService bestPayService;

    @Autowired
    OrderService orderService;
    private static final String orderName = "dream商城订单";

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderName(orderName);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("[微信订单支付]，payRequest：{}", JsonUtil.toJson(payRequest));
//        因微信号没有，因此不能真正支付，所以模拟支付
//        真正支付：
//        PayResponse payResponse = bestPayService.pay(payRequest);
//        模拟支付：
        PayResponse payResponse = new PayResponse();
        payResponse.setAppId("xxxAppId");
        payResponse.setOrderId(orderDTO.getOrderId());
        payResponse.setOrderAmount(orderDTO.getOrderAmount().doubleValue());

        log.info("[微信订单支付]，payResponse：{}", JsonUtil.toJson(payResponse));
        return payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1.校验签名
        //2.检查支付结果
        //3.金额对比
        //4.支付人和下单人是否相等
        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("[微信支付异步通知]，payResponse：{}", JsonUtil.toJson(payResponse));
        OrderDTO order = orderService.findOne(payResponse.getOrderId());
        if (order == null) {
            log.error("[微信支付异步通知] 订单不存在，orderId：{}", payResponse.getOrderId());
            throw new SellException(ResponseCodeEnum.ORDER_NOT_EXIST);
        }
        if (!BigDecimalUtil.equal(order.getOrderAmount().doubleValue(), payResponse.getOrderAmount())) {
            log.error("[微信支付异步通知] 金额不相等，orderId：{}，订单金额：{} 支付宝通知金额：{}", payResponse.getOrderId(), order.getOrderAmount(), payResponse.getOrderAmount());
            throw new SellException(ResponseCodeEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        //修改订单支付状态
        orderService.paid(order);
        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("[微信退款],refundRequest:{}",JsonUtil.toJson(refundRequest));
        RefundResponse refundResponse = bestPayService.refund(refundRequest);
        log.info("[微信退款],refundResponse:{}",JsonUtil.toJson(refundResponse));
        return refundResponse;
    }
}
